package LC;

import java.util.Stack;

/**
 * @ClassName LC20
 * @Description: TODO
 * @Author hl
 * @Date 2020/9/13
 * @Version V1.0
 **/
public class LC20 {
    public static void main(String[] args) {

    }
    public boolean isValid(String s) {
        if(s==null){
            return false;
        }

        if(s.length()==0){
            return true;
        }

        Stack<Character> stack=new Stack<>();
        for(int i=0;i<s.length();i++){
            char target=s.charAt(i);
            if(target=='('||target=='{'||target=='['){
                 stack.push(target);
            }else{
                if(stack.isEmpty()){
                    return  false;
                }else{
                    char check=stack.pop();
                    char match;
                    if(check==')'){
                        match='(';
                    }else{
                        if(check==']'){
                            match='[';
                        }else{
                            match='{';
                        }
                    }

                    if(match!=target){
                        return  false;
                    }
                }

            }
        }
        return  stack.size()==0;

    }
}
